﻿using Architecture.API.Application.TraceabilityCommand.PictureCommand;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Traceability.Domain;

namespace Architecture.API.Controllers
{
    /// <summary>
    /// API控制器--图片
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PictureManagementController : ControllerBase
    {
        /// <summary>
        /// 命令总线
        /// </summary>
        protected readonly IMediator mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">命令总线</param>
        public PictureManagementController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 添加图片信息
        /// </summary>
        /// <param name="command">命令请求</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddPictures(PictureCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code=await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 获取图片列表
        /// </summary>
        /// <param name="command">请求</param>
        /// <returns>返回图片列表集合</returns>
        [HttpPost]
        public async Task<ApiResult<List<Pictures>>> GetPicturesList(PictureSeachCommand command)
        {
            ApiResult<List<Pictures>> result = new ApiResult<List<Pictures>>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 反填图片信息
        /// </summary>
        /// <param name="command">命令请求</param>
        /// <returns>返回图片信息</returns>
        [HttpPost]
        public async Task<ApiResult<Pictures>> GetPicturesModels(PictureReverseFillingCommand command)
        {
            ApiResult<Pictures> result = new ApiResult<Pictures>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 修改图片信息
        /// </summary>
        /// <param name="command">命令请求</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdatePictures(PictureUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 删除图片信息
        /// </summary>
        /// <param name="command">命令请求</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeletedPictures(PictureRemoveCommad command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="file">要上传的文件信息</param>
        /// <returns>上传后的文件路径</returns>
        [HttpPost]
        public ApiResult<string> UploadFile(IFormFile file)
        {
            ApiResult<string> result = new ApiResult<string>();
            try
            {
                //上产后的文件名(包含全路径)
                string fileName =Directory.GetCurrentDirectory()+@"\wwwroot\picture\"+file.FileName;
                //上传
                using (FileStream fs =new FileStream(fileName, FileMode.Create))
                {
                    file.CopyTo(fs);
                    fs.Close();
                }
                result.Data = "http://localhost:5063/picture/" + file.FileName;
                return result;
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
        }
    }
}
